/**
 * TagDaoImp.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-11-26 17:40:14 +0800 $
 * $Revision: 1266 $
 */
package com.myspace.dao.hibernate;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.myspace.dao.UserDao;
import com.myspace.domain.User;
import com.myspace.common.exception.ErrorPasswordException;
import com.myspace.common.exception.UserNotFoundException;

/**
 * Dao implements for UserDaoImp.
 *
 * @author Allen.Chen
 */
public class UserDaoImp extends GenericDaoImp<User, Long> implements UserDao
{
	@SuppressWarnings("unchecked")
	public User findByNameAndPassword(User us) throws UserNotFoundException,
			ErrorPasswordException
	{
		String hql = "select u from User u where u.name=?";
		List users = this.getHibernateTemplate().find(hql, us.getName());
		if (users == null || users.isEmpty())
		{
			throw new UserNotFoundException();
		}
		User user = (User) users.get(0);
		if (!us.getPassword().trim()
				.equalsIgnoreCase(user.getPassword().trim()))
		{
			throw new ErrorPasswordException();
		}
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> getHot()
	{
		return getHibernateTemplate().executeFind(new HibernateCallback()
		{
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Criteria criteria = session.createCriteria(User.class);
				criteria.setMaxResults(10);
				criteria.addOrder(Order.desc("clicks"));
				List<User> list = (List<User>) criteria.list();
				return list;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<User> getLastUpdated()
	{
		return getHibernateTemplate().executeFind(new HibernateCallback()
		{
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Criteria criteria = session.createCriteria(User.class);
				criteria.setMaxResults(10);
				criteria.addOrder(Order.desc("updateDate"));
				List<User> list = (List<User>) criteria.list();
				return list;
			}
		});
	}

	public User findByName(String friendName) throws UserNotFoundException
	{
		String hql = "select u from User u where u.name=?";
		List users = this.getHibernateTemplate().find(hql, friendName);
		if (users == null || users.isEmpty())
		{
			throw new UserNotFoundException();
		}
		return (User) users.get(0);
	}

	@SuppressWarnings("unchecked")
	public Map<Long, User> findbyIds(Long[] ids)
	{
		if(ids==null)
			return null;
		Map<Long, User> users = null;
		String hql="from User u where u.id ";
		hql=this.prepareString(hql, ids.length);
		List<User> us=this.getHibernateTemplate().find(hql, ids);
		if(us!=null&&!us.isEmpty()){
			users=new HashMap<Long, User>();
			for(User user:us){
				users.put(user.getId(), user);
			}
		}
		return users;
	}

	public String prepareString(String sql, int length)
	{
		if (length <= 0)
			return null;
		StringBuffer sb = new StringBuffer(sql);
		if (length == 1)
			sb.append("=?");
		else
		{
			sb.append(" in(?");
			for (int i = 0; i < length - 1; i++)
			{
				sb.append(",?");
			}
			sb.append(") ");
		}
		return sb.toString();
	}
}
